//
//  SSRefreshView.swift
//  刷新控件
//
//  Created by 叶竞泽 on 2017/1/29.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

enum RefreshState {
    case Normal
    case Pulling
    case WillRefresh
}

/// 刷新视图 - 负责刷新相关的UI显示和动画显示
class SSRefreshView: UIView {

    var animationDuration: TimeInterval = 0.25
    
    /// 刷新状态
    var refreshState: RefreshState = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                tipLabel?.text = "下拉开始刷新..."
                tipIcon?.isHidden = false
                tipIndicator?.stopAnimating()
                UIView.animate(withDuration: animationDuration, animations: {
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
            case .Pulling:
                tipLabel?.text = "放手开始刷新..."
                UIView.animate(withDuration: animationDuration, animations: {
                    // UIView封装的旋转采用就近原则,默认顺时针.
                    // 如果要实现360°旋转,需要CABaseAnimation
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.001))
                })
            case .WillRefresh:
                tipLabel?.text = "正在努力加载..."
                // 隐藏提示图标
                tipIcon?.isHidden = true
                tipIndicator?.startAnimating()
            }
        }
    }
    
    /// 类方法,由nib返回一个SSRefreshView
    ///
    /// - Returns: SSRefreshView类型实例
    class func refreshView() -> SSRefreshView? {
        
        let refreshView = UINib(nibName: "SSMeituanRefreshView", bundle: nil).instantiate(withOwner: nil, options: nil).first as? SSRefreshView
        
        return refreshView
    }
    
    var parentHeight: CGFloat = 0
    
    /// 提示图标
    @IBOutlet weak var tipIcon: UIImageView?
    
    /// 文字视图
    @IBOutlet weak var tipLabel: UILabel?
    
    /// 指示器
    @IBOutlet weak var tipIndicator: UIActivityIndicatorView?
}
